//
//  SHKViewController.m
//  HLEEmptyView
//
//  Created by ldb on 09/12/2020.
//  Copyright (c) 2020 ldb. All rights reserved.
//

#import "SHKViewController.h"
#import <ReactiveObjC/ReactiveObjC.h>
#import <HLEEmptyView/HLEEmptyView.h>
#import <Masonry/Masonry.h>
#import <YYCategories/YYCategories.h>

@interface SHKViewController () <UITableViewDelegate, UITableViewDataSource>

@property(nonatomic, strong)  UITableView *tableView;

@property(nonatomic, strong) NSMutableArray *dataSourceArray;
@end

@implementation SHKViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    [self.view addSubview:self.tableView];
    
    
    UIButton *addDataBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [addDataBtn setTitle:@"增加数据" forState:UIControlStateNormal];

    [[addDataBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        [self.dataSourceArray addObject:@"增加数据"];
        [self.tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:self.dataSourceArray.count-1 inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
    }];
    addDataBtn.backgroundColor = UIColor.purpleColor;
    [self.view addSubview:addDataBtn];
    
    [addDataBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        if (@available(iOS 11.0, *)) {
            make.bottom.mas_equalTo(self.view.mas_safeAreaLayoutGuideBottom);
        } else {
            // Fallback on earlier versions
        }
        make.size.mas_equalTo(CGSizeMake(100, 60));
    }];
    
    UIButton *deleteDataBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [deleteDataBtn setTitle:@"删除数据" forState:UIControlStateNormal];
     [[deleteDataBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
         [self.dataSourceArray removeFirstObject];
         [self.tableView deleteRow:0 inSection:0 withRowAnimation:UITableViewRowAnimationFade];
     }];
    deleteDataBtn.backgroundColor = UIColor.purpleColor;

     [self.view addSubview:deleteDataBtn];
     
     [deleteDataBtn mas_makeConstraints:^(MASConstraintMaker *make) {
         make.right.mas_equalTo(0);
         if (@available(iOS 11.0, *)) {
             make.bottom.mas_equalTo(self.view.mas_safeAreaLayoutGuideBottom);
         } else {
             // Fallback on earlier versions
         }
         make.size.mas_equalTo(CGSizeMake(100, 60));
     }];
    
    
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.mas_equalTo(0);
        make.bottom.mas_equalTo(addDataBtn.mas_top);
    }];
    
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.tableView hle_showNoInterNetView];
    });
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSourceArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (NSMutableArray *)dataSourceArray
{
    if (!_dataSourceArray) {
        _dataSourceArray = @[].mutableCopy;
    }
    return _dataSourceArray;
}

- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableFooterView = UIView.new;
        _tableView.emptyView = [HLEEmptyDataView emptyViewWithImage:[UIImage imageNamed:@"编组 4"] titleStr:@"songhong"];
        @weakify(self);
        _tableView.noInternetView = [HLENoInternetView noInternetViewWithImage:[UIImage imageNamed:@"编组 4"] titleStr:@"网络异常, 请检查网络状态" reloadDataStr:@"重新加载" reloadClick:^{
            @strongify(self);
            [self.tableView hle_hiddenNoInterNetView];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                @strongify(self);
                [self.tableView reloadData];
            });
        }];
        
    }
    return _tableView;
}
@end
